package com.hello.sandbox.proxy.record;

import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.os.IBinder;
import com.hello.sandbox.utils.compat.BundleCompat;

/** Created by Milk on 4/1/21. * ∧＿∧ (`･ω･∥ 丶　つ０ しーＪ 此处无Bug */
public class ProxyServiceRecord {
  public Intent mServiceIntent;
  public ServiceInfo mServiceInfo;
  public IBinder mToken;
  public int mUserId;
  public int mStartId;

  public ProxyServiceRecord(
      Intent serviceIntent, ServiceInfo serviceInfo, IBinder token, int userId, int startId) {
    mServiceIntent = serviceIntent;
    mServiceInfo = serviceInfo;
    mUserId = userId;
    mStartId = startId;
    mToken = token;
  }

  public static void saveStub(
      Intent shadow,
      Intent target,
      ServiceInfo serviceInfo,
      IBinder token,
      int userId,
      int startId) {
    shadow.putExtra("_B_|_target_", target);
    shadow.putExtra("_B_|_service_info_", serviceInfo);
    shadow.putExtra("_B_|_user_id_", userId);
    shadow.putExtra("_B_|_start_id_", startId);
    BundleCompat.putBinder(shadow, "_B_|_token_", token);
  }

  public static ProxyServiceRecord create(Intent intent) {
    Intent target = intent.getParcelableExtra("_B_|_target_");
    ServiceInfo serviceInfo = intent.getParcelableExtra("_B_|_service_info_");
    int userId = intent.getIntExtra("_B_|_user_id_", 0);
    int startId = intent.getIntExtra("_B_|_start_id_", 0);
    IBinder token = BundleCompat.getBinder(intent, "_B_|_token_");
    return new ProxyServiceRecord(target, serviceInfo, token, userId, startId);
  }
}
